//
//  ChangePasswardController.m
//  GuShiNews
//
//  Created by 丁一 on 2019/8/19.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "ChangePasswardController.h"
#import "JonesClass.h"

@interface ChangePasswardController ()<UITextFieldDelegate>

@property (nonatomic, strong) NSArray *textarray;

@property (nonatomic, strong) UITextField *textn001;
@property (nonatomic, strong) UITextField *textn002;
@property (nonatomic, strong) UITextField *textn003;
@property (nonatomic, strong) UIButton    *XiyiButton;

@end

@implementation ChangePasswardController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"修改密码";
    _textarray = [[NSArray alloc] initWithObjects:@"请输入旧密码",@"请输入新密码",@"请确认密码", nil];
    self.view.backgroundColor = LineColor;
    [self loadhere];
}

- (void)loadhere{
    [_textarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *textbg            = [[UIView alloc] initWithFrame:CGRectMake(25, NAV_Height+15+65*idx, RECRWITCH-50, 50)];
        textbg.layer.cornerRadius  = 5;
        textbg.layer.masksToBounds = YES;
        textbg.backgroundColor    = [UIColor whiteColor];
        [self.view addSubview:textbg];
        
        UITextField *textfield    = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, textbg.frame.size.width-20, textbg.frame.size.height-10)];
        textfield.textColor       = [UIColor blackColor];
        textfield.font            = [UIFont systemFontOfSize:17];
        [textfield setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        textfield.backgroundColor = [UIColor whiteColor];
        textfield.placeholder     = obj;
        textfield.tag             = idx;
        textfield.delegate        = self;
        textfield.layer.borderWidth = 0;
        textfield.secureTextEntry = YES;
        [textbg addSubview:textfield];
        
        if (idx==0) {
            self.textn001             = textfield;
        }else if (idx==1){
            self.textn002              = textfield;
        }else{
            self.textn003              = textfield;
        }
    }];
    
    UIButton *button           = [[UIButton alloc] initWithFrame:CGRectMake(25, NAV_Height + 215, RECRWITCH-50, 45)];
    button.tag=1;
    [button setTitle:@"确认" forState:UIControlStateNormal];
    [button setTitleColor:NavItemTitleColor forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    [button addTarget:self action:@selector(go_done:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}

- (void)go_done:(UIButton *)sender{
    if (sender.tag==1) {
        if (![[JonesClass md5:self.textn001.text] isEqualToString:[DEFAULTS objectForKey:@"password"]]) {
            [SVProgressHUD showErrorWithStatus:@"旧密码不正确"];
        }
        else if (self.textn002.text.length<1||self.textn002.text==nil){
            [SVProgressHUD showErrorWithStatus:@"请填写密码"];
        }
        else if (![JonesClass checkPassWord:self.textn002.text]){
            [SVProgressHUD showErrorWithStatus:@"密码格式不对"];
        }else if (![self.textn002.text isEqualToString:self.textn003.text]){
            [SVProgressHUD showErrorWithStatus:@"确认密码不一致"];
        }else{
            
            _usermodle.password = [JonesClass md5:self.textn002.text];
            [self goback];
            
        }
    }else{
        
    }
}

@end
